package com.fmboy.MXH.nav.controller;

import com.fmboy.MXH.nav.domain.AdminRequired;
import com.fmboy.MXH.nav.domain.ErrorCode;
import com.fmboy.MXH.nav.domain.Result.Result;
import com.fmboy.MXH.nav.domain.dto.NavigationQueryDTO;
import com.fmboy.MXH.nav.domain.pojo.NavigationVO;
import com.fmboy.MXH.nav.service.NavigationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@AdminRequired
@RestController
@RequestMapping("/mxh-nav/admin/navigation")
@Tag(name = "管理员导航接口", description = "adminNavigation API")
public class AdminNavigationController {

    //获取-导航
    @Resource
    private NavigationService navigationService;

    //获取-导航
    @GetMapping
    @Operation(summary = "获取-导航", description = "获取-导航")
    public Result<List<NavigationVO>> getNavigation(
            @RequestParam(required = false) String navigation_name,
            @RequestParam(required = false) Integer category_id,
            @RequestParam(required = false) String state
    ) {
        NavigationQueryDTO navigationQueryDTO = new NavigationQueryDTO();
        navigationQueryDTO.setNavigation_name(navigation_name);
        navigationQueryDTO.setCategory_id(category_id);
        navigationQueryDTO.setState(state);
        try {
            List<NavigationVO> navigation = navigationService.AdminGetNavigation(navigationQueryDTO);
            return Result.success("获取导航列表成功！", navigation);
        } catch (RuntimeException e) {
            return Result.error(ErrorCode.UNAUTHORIZED, e.getMessage());
        }
    }

    //获取-导航详情
    @GetMapping("/info")
    @Operation(summary = "获取-导航详情", description = "获取-导航详情")
    public Result<NavigationVO> getNavigationDetail(@RequestParam Integer id) {
        try {
            NavigationVO navigation = navigationService.getNavigationById(id);
            return Result.success("获取导航详情成功！", navigation);
        } catch (RuntimeException e) {
            return Result.error(ErrorCode.UNAUTHORIZED, e.getMessage());
        }
    }

    //修改-导航
    @Operation(summary = "修改-导航", description = "修改-导航")
    @PutMapping
    public Result<String> updateNavigation(@RequestBody NavigationQueryDTO navigationQueryDTO) {
        try {
            navigationService.AdminUpdateNavigationById(navigationQueryDTO);
            return Result.success("修改导航成功！");
        } catch (RuntimeException e) {
            return Result.error(ErrorCode.UNAUTHORIZED, e.getMessage());
        }
    }

    //修改-导航发布状态
    @PutMapping("/state")
    @Operation(summary = "修改-导航发布状态", description = "修改-导航发布状态")
    public Result<String> updateNavigationState(@RequestBody NavigationQueryDTO navigationQueryDTO) {
        try {
            navigationService.AdminUpdateNavigationState(navigationQueryDTO);
            return Result.success("修改导航发布状态成功！");
        } catch (RuntimeException e) {
            return Result.error(ErrorCode.UNAUTHORIZED, e.getMessage());
        }
    }

    //删除-导航
    @DeleteMapping
    @Operation(summary = "删除-导航", description = "删除-导航")
    public Result<String> deleteNavigation(@RequestParam Integer id) {
        try {
            navigationService.AdminDeleteByNavigationId(id);
            return Result.success("删除成功！");
        } catch (RuntimeException e) {
            return Result.error(ErrorCode.UNAUTHORIZED, e.getMessage());
        }
    }
}
